package com.sz.app.customer.api.logistics;

import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.constants.MessagesCodes;
import com.sz.biz.common.customer.dto.CusUserAddressDto;
import com.sz.biz.common.customer.service.CustomerUserAddressService;
import com.sz.biz.logistics.base.service.AirportService;
import com.sz.biz.logistics.base.service.PortService;
import com.sz.biz.logistics.constants.BaseConstants;
import com.sz.biz.logistics.core.dto.ProductGroupDto;
import com.sz.biz.logistics.core.service.ProductGroupService;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.service.PrincipalUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 客户地址 <br>
 * Author: qiankun <br>
 * Date: 2017-05-12 09:52:51
 */
@Controller
@RequestMapping("${api}/customer/useraddress")
@Api(description = " ", tags = "037-006、客户信息管理-客户地址")
public class CusUserAddressController extends BaseController {

    @Autowired
    private CustomerUserAddressService customerUserAddressService;
    @Autowired
    private AirportService airportService;
    @Autowired
    private PortService portService;
    @Autowired
    private ProductGroupService productGroupService;

    @ApiOperation(value = "查询地址", notes = "查询地址")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "地址类型,1、发货地址 2、收货地址", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "transportType", value = "全球海运:1,全球速递:11,全球空运:6", required = false, dataType = "int", paramType = "query")})
    @RequestMapping(value = "/search", method = {RequestMethod.GET})
    @ResponseBody
    public ResultDto findUserAddress(@RequestParam(required = false) Integer id, @RequestParam(required = false) Integer type, @RequestParam(required = false) Integer transportType) {
        ResultDto result = new ResultDto();
        int cusUserId = PrincipalUtils.getAccountId();
        List<CusUserAddressDto> lists = customerUserAddressService.queryByCusIdAndIdsAndType(id, cusUserId, type, transportType);
        result.setData(lists);
        return result;
    }

    @ApiOperation(value = "保存或者修改", notes = "保存修改<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressDto", value = "提交地址信息 ", required = true, dataType = "CusUserAddressDto", paramType = "body")})
    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.POST})
    @ResponseBody
    public ResultDto saveOrUpdateUserAddress(@RequestBody @Validated CusUserAddressDto addressDto) {
        addressDto.setCustomerId(PrincipalUtils.getAccountId());
        int result = customerUserAddressService.saveOrUpdate(addressDto);
        ResultDto dto = null;
        if (addressDto.getId() != null && addressDto.getId() > 0) {
            dto = getOperateSuccessDto(MessagesCodes.MODULE_CUSTOMER_ADDRESS_UPDATE_SUCCESS);
        } else {
            dto = getOperateSuccessDto(MessagesCodes.MODULE_CUSTOMER_ADDRESS_ADD_SUCCESS);
        }
        dto.setData(result);
        return dto;
    }

    @ApiOperation(value = "删除地址", notes = "删除地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ids", required = false, dataType = "List", paramType = "body")})
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    public ResultDto deleteByAddressId(@RequestBody(required = false) List<Integer> ids) {
        int cusUserId = PrincipalUtils.getAccountId();
        customerUserAddressService.delete(ids, cusUserId);
        ResultDto resultDto = this
                .getOperateSuccessDto(MessagesCodes.MODULE_CUSTOMER_ADDRESS_DELETE_SUCCESS);
        return resultDto;
    }

    @ApiOperation(value = "设置默认地址", notes = "设置默认地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldId", value = "oldId", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "newId", value = "newId", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "地址类型", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "transportType", value = "全球海运:1,全球速递:11,全球空运:6", required = true, dataType = "int", paramType = "query")})
    @RequestMapping(value = "/setdefault", method = {RequestMethod.POST})
    @ResponseBody
    public ResultDto setDefault(@RequestParam(required = false) Integer oldId,
                                @RequestParam(required = false, defaultValue = "0") Integer newId,
                                @RequestParam(required = false, defaultValue = "0") Integer type,
                                @RequestParam(required = false, defaultValue = "0") Integer transportType) {
        int cusUserId = PrincipalUtils.getAccountId();
        customerUserAddressService.setDefault(oldId, newId, cusUserId, type, transportType);
        ResultDto resultDto = this
                .getOperateSuccessDto(MessagesCodes.MODULE_CUSTOMER_ADDRESS_SET_DEFAULT_ADDRESS_SUCCESS);
        resultDto.setData(newId);
        return resultDto;
    }

    @ApiOperation(value = "查询是否已经达到客户地址数据", notes = "查询是否已经达到客户地址数量")
    @RequestMapping(value = "/query/addresscount", method = {RequestMethod.GET})
    @ResponseBody
    public ResultDto queryAddressCount(@RequestParam(required = false) Integer type, @RequestParam(required = false) Integer transportType) {
        ResultDto resultDto = new ResultDto();
        int cusUserId = PrincipalUtils.getAccountId();
        int count = customerUserAddressService.queryCountByCustomerIdAndType(cusUserId, type, transportType);
        resultDto.setData(count);
        return resultDto;
    }

    /**
     * 分页查询机场或港口
     *
     * @throws Exception
     */
    @ApiOperation(value = "查询机场或港", notes = "查询机场或港<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型：1为港口，6为机场", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/shortlist", method = {RequestMethod.GET})
    public
    @ResponseBody
    ResultDto findShortList(@RequestParam(required = false) String q, @RequestParam(required = true) String type,
                            @RequestParam(required = false, defaultValue = "0") int pageIndex, @RequestParam(required = false, defaultValue = "10") int pageSize) {
        ResultDto dto = null;
        q = StringUtils.replaceSqlPattern(q);
        if (BaseConstants.TransportType.AIR.equals(type)) {
            dto = airportService.shortAirportListPage(new QueryParams(pageSize, pageIndex, q, null, false));
        } else if (BaseConstants.TransportType.PORT.equals(type)) {
            dto = portService.shortPortListPage(new QueryParams(pageSize, pageIndex, q, null, false));
        }
        return dto;
    }

    /**
     * 根据parentId查询产品组
     *
     * @throws Exception
     */
    @ApiOperation(value = "搜索产品组", notes = "搜索产品组<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父id", dataType = "int", paramType = "query")})
    @RequestMapping(value = "/product/group/get", method = {RequestMethod.GET})
    @ResponseBody
    public ResultDto findByParentId(@RequestParam(required = true, defaultValue = "0") int parentId) {
        List<ProductGroupDto> list = productGroupService.findByParentId(parentId);
        ResultDto dto = new ResultDto();
        dto.setData(list);
        return dto;
    }

    /**
     * 查询运输类型
     *
     * @throws Exception
     */
    @ApiOperation(value = "查询运输类型", notes = "查询运输类型<br>权限：")
    @ApiImplicitParams({})
    @RequestMapping(value = "/product/group/type", method = {RequestMethod.GET})
    @ResponseBody
    public ResultDto findTransportType() {
        List<ProductGroupDto> list = productGroupService.findTransportType();
        ResultDto dto = new ResultDto();
        dto.setData(list);
        return dto;
    }

    @ApiOperation(value = "下单时，查询地址", notes = "查询地址(下单时)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1、发货地址 2、收货地址,3揽收地址", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "transportType", value = "全球海运:1,全球速递:11,全球空运:6", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "uId", value = "产品Id", required = true, dataType = "String", paramType = "query")})
    @RequestMapping(value = "/product/search", method = {RequestMethod.GET})
    @ResponseBody
    public ResultDto searchOrderAddress(@RequestParam(required = false) Integer type, @RequestParam Integer transportType, @RequestParam(required = true) String uId) {
        ResultDto resultDto = new ResultDto();
        int cusUserId = PrincipalUtils.getAccountId();
        Map<String, List<CusUserAddressDto>> resultMap = customerUserAddressService.findAddressByProduct(cusUserId, type, transportType, uId);
        resultDto.setData(resultMap);
            return resultDto;
    }
}
